<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<p>修改密码</p>
		</header>

		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					原密码：
				</div>
				<div class="content">
					<input type="password" v-model="oldPassword" placeholder="请输入原密码">
				</div>
			</li>
			<li>
				<div class="title">
					新密码：
				</div>
				<div class="content">
					<input type="password" v-model="newPassword" placeholder="请输入新密码">
				</div>
			</li>
			<li>
				<div class="title">
					确认密码：
				</div>
				<div class="content">
					<input type="password" v-model="confirmPassword" placeholder="请再次输入新密码">
				</div>
			</li>
		</ul>

		<div class="button-submit">
			<button @click="submit">确认修改</button>
		</div>
		<div class="button-cancel">
			<button @click="cancel">取消</button>
		</div>

		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>

<script>
	import Footer from '../components/Footer.vue';

	export default {
		name: 'PasswordChange',
		data() {
			return {
				oldPassword: '',
				newPassword: '',
				confirmPassword: '',
				user: {},
				username: '',
			}
		},



		methods: {
			submit() {
				// 表单验证
				if (this.oldPassword === '') {
					alert('原密码不能为空！');
					return;
				}
				if (this.newPassword === '') {
					alert('新密码不能为空！');
					return;
				}
				if (this.newPassword.length < 4) {
					alert('新密码长度不能少4位！');
					return;
				}
				if (this.confirmPassword === '') {
					alert('请确认新密码！');
					return;
				}
				if (this.newPassword !== this.confirmPassword) {
					alert('两次输入的新密码不一致！');
					return;
				}
				const token = localStorage.getItem('id_token');
				console.log(this.newPassword)
				console.log(this.oldPassword)
				this.$axios.post('password2', {
					newPassword: this.newPassword,
					oldPassword: this.oldPassword
				}, {
					headers: {
						Authorization: `Bearer ${token}`
					}
				}).then(response => {
					const result = response.data;
					if (result.success && result.code === 200) {
						alert('密码修改成功，请重新登录！');
						this.$router.push('/login');
					}
				}).catch(error => {
					console.error(error);
					alert('修改失败：' + (error.response?.data?.message || '原密码不正确或网络错误'));
				})
			},
			cancel() {
				this.$router.go(-1);
			},
		},
		components: {
			Footer
		}
	}
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
		user-select: none;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
		user-select: none;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-submit {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-submit button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;

		border: none;
		outline: none;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .button-cancel {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;

	}

	.wrapper .button-cancel button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #666;
		background-color: #EEE;
		border: solid 1px #DDD;
		border-radius: 4px;

		border: none;
		outline: none;
		user-select: none;
		cursor: pointer;
	}

	/****************** 底部菜单部分 ******************/
	.wrapper .footer {
		width: 100%;
		height: 14vw;
		border-top: solid 1px #DDD;
		background-color: #fff;

		position: fixed;
		left: 0;
		bottom: 0;

		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.wrapper .footer li {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		color: #999;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .footer li p {
		font-size: 2.8vw;
	}

	.wrapper .footer li i {
		font-size: 5vw;
	}
</style>